package com.baoming.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.ISiteDao;
import com.baoming.domain.Site;
import com.baoming.service.ISiteService;

/**
 * 站点服务组件
 * @author Administrator
 *
 */
@Transactional(readOnly=true)
@Service(ISiteService.SERVICE)
public class SiteServiceImpl implements ISiteService {

	@Resource(name=ISiteDao.SERVICE)
	private ISiteDao siteDao ;

	/**
	 * 根据会员建立一个新站点
	 */ 
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void buildSite(Site site) {
		// TODO Auto-generated method stub 
		siteDao.save(site) ; 
	}

	/**
	 * 查询用户站点信息
	 * @see com.baoming.service.ISiteService#findSiteByMember(java.lang.String)
	 */
	public Site findSiteByMember(String memberId) {
		// TODO Auto-generated method stub
		String hql = " and m.memberId = ? " ;
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		List<Site> sites = siteDao.findCollectionByConditionNoPage(hql, params.toArray(), null) ; 
		if(sites != null && sites.size()==1){
			return sites.get(0) ; 
		}
		return null;
	}

	/**
	 * 更新站点信息
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void modifySite(Site siteModel) {
		// TODO Auto-generated method stub
		siteDao.update(siteModel) ; 
	}

	public Site findSite(String siteId) {
		// TODO Auto-generated method stub
		return siteDao.findObjectById(siteId) ;
	}

	/**
	 * 通过唯一标识码查询站点
	 */
	public Site findSiteByCode(String siteCode) {
		// TODO Auto-generated method stub 
		String hql = " and m.siteCode = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(siteCode) ; 
		List<Site> sitelist = siteDao.findCollectionByConditionNoPage(hql, params.toArray(), null) ; 
		if(sitelist != null && sitelist.size() == 1){
			return sitelist.get(0) ; 
		}
		return null;
	}

	/**
	 * 更新站点标识码
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateSiteCode(String siteCode , String id) {
		// TODO Auto-generated method stub
		siteDao.updateSiteCode(siteCode,id) ;
	} 
	
}
